/*****************************************************
** Author:
** Date: 2022-01-12
*
*****************************************************/
#include <stdio.h>
#include <string.h>
// #include <sys/time.h>
#include "pcf8575.h"

#define log_dbg(_fmt_, ...)   \
            printf("[PCF8575][%s:%d][%s][DBG] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define log_inf(_fmt_, ...)   \
            printf("[PCF8575][%s:%d][%s][INF] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define log_wrn(_fmt_, ...)   \
            printf("[PCF8575][%s:%d][%s][WRN] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)
#define log_err(_fmt_, ...)   \
            printf("[PCF8575][%s:%d][%s][ERR] "_fmt_"\r\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__)

/**
 * @brief
 * @param  None
 * @retval None
 */
int pcf8575_read_io(pcf8575_obj_t *obj, unsigned short *io)
{
    unsigned char val[2];
    int ret;

    if (obj == NULL) {
        return -1;
    }

    ret = obj->read(val, sizeof(val));
    if (ret != 0) {
        log_wrn("val [0] %#x [1] %#x", val[0], val[1]);
        return ret;
    }

    *io = val[0];
    *io |= val[1] << 8;
    return 0;
}

int pcf8575_write_io(pcf8575_obj_t *obj, unsigned short io)
{
    unsigned char val[2];

    if (obj == NULL) {
        return -1;
    }

    val[0] = io & 0x00ff;
    val[1] = (io & 0xff00) >> 8;

    return obj->write(val, sizeof(val));
}

/* End of file */
